﻿using System;
using System.Data;
using System.Windows.Forms;
using DocPlat.Library;
using DataGridViewAutoFilter;

namespace DocPlat.UI
{
    public partial class DocPlatHistory : UserControl
    {
        ServerObject mServerObject = null;

        public DocPlatHistory()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }      

        private void closeButton_Click(object sender, EventArgs e)
        {
            mServerObject.CloseThisTab(this);
        }

        private void Remains_Load(object sender, EventArgs e)
        {
            FillRemainsGridView();
        }

        private void FillRemainsGridView()
        {
            BindingSource bindigSource = new BindingSource(mServerObject.GetDocPlatList(), null);
            historyGridView.DataSource = bindigSource;

            if (historyGridView.DataSource != null)
            {
                historyGridView.Columns["id"].Visible = false;
                //historyGridView.Columns["ID Banca"].Visible = false;

                foreach (DataGridViewColumn col in historyGridView.Columns)
                {
                    col.HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);
                }

                historyGridView.AutoResizeColumns();
            }
        }

        private void newPaymentButton_Click(object sender, EventArgs e)
        {
            int newNrBonPlata = 0;
            if (mServerObject.GetNewNrBonPlat(out newNrBonPlata))
            {
                PaymentFormInputPanel FormInput = new PaymentFormInputPanel();
                FormInput.NrBonDePlata = newNrBonPlata;

                FormInput.ShowDialog();

                if (FormInput.OpenPrintFormAfterSave)
                {
                    ShowPrinForm(FormInput.NrBonDePlata);
                }
            }

            FillRemainsGridView();
        }

        private void historyGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int newNrBonPlata = 0;

            if (historyGridView.SelectedRows.Count == 1)
            {
                int.TryParse(historyGridView.Rows[historyGridView.SelectedRows[0].Index].Cells["id"].Value.ToString(), out newNrBonPlata);

                PaymentFormInputPanel FormInput = new PaymentFormInputPanel();
                FormInput.EditOperation = true;
                FormInput.NrBonDePlata = newNrBonPlata;

                FormInput.ShowDialog();           
            }

            FillRemainsGridView();
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            FillRemainsGridView();
        }

        private void ShowPrinForm(int idDocPlat)
        {
            PrintForm printForm = new PrintForm();

            DataTable docPlat = mServerObject.GetDocPlatByID(idDocPlat);
            mServerObject.GetServicesListForPrint(idDocPlat);

            if (docPlat != null && docPlat.Rows.Count == 1)
            {
                printForm.BonDePlataNr = docPlat.Rows[0]["id"].ToString();
                DateTime dinData = new DateTime(1000, 1, 1);
                DateTime.TryParse(docPlat.Rows[0]["date"].ToString(), out dinData);
                printForm.DinData = dinData.ToString("dd.MM.yyyy");
                printForm.DenumireaBeneficiaruluiSiCodFiscal = docPlat.Rows[0]["Company Name"].ToString() + ", codul fiscal: " + docPlat.Rows[0]["cod_fisc_benef"].ToString();
                printForm.DenumireaBanca = docPlat.Rows[0]["Banc Description"].ToString();
                printForm.Contul = docPlat.Rows[0]["Cont"].ToString();
                printForm.Platitotul = docPlat.Rows[0]["FirstName"].ToString() + " " + docPlat.Rows[0]["LastName"].ToString() + " " + docPlat.Rows[0]["Patronimic"].ToString();
                printForm.IDNO = docPlat.Rows[0]["idnoPlatitor"].ToString();
                printForm.ServiciiPrestateDe = docPlat.Rows[0]["Company Name"].ToString();
                printForm.Adresa = docPlat.Rows[0]["Address"].ToString();
                printForm.SumaTotala = docPlat.Rows[0]["summaTotala"].ToString();

                printForm.ShowDialog();
            }
        }

        private void printPayButton_Click(object sender, EventArgs e)
        {
            if (historyGridView.SelectedRows.Count == 1)
            {
                int idDocPlat = 0;
                int.TryParse(historyGridView.Rows[historyGridView.SelectedRows[0].Index].Cells["id"].Value.ToString(), out idDocPlat);

                 ShowPrinForm(idDocPlat);
            }
        }

        

        



    }
}
